from django.shortcuts import render
import pandas as pd
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
@csrf_exempt
def upload_excel(request):
    if request.method == 'POST':
        # 获取上传的文件
        excel_file = request.FILES['file']
        # 解析 Excel 文件
        df = pd.read_excel(excel_file)

        # 将每一列转换为 JSON 格式的键值对
        data_dict = df.to_dict(orient='list')

        # 返回属性及其对应的值给前端
        return JsonResponse({'data': data_dict})
    return JsonResponse({'error': 'Invalid request'}, status=400)
